﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ActivePalInterop.Data;
using ActivePalInterop.Events;

namespace ActivePal
{
    /// <summary>
    /// Interaction logic for PersonalData.xaml
    /// </summary>
    public partial class PersonalDataWindow : Window
    {
        #region Event DebugMessage

        /// <summary>
        /// Generates a debug message that can be added to a GUI output
        /// </summary>
        public event StaticMessageEventHandler DebugMessage;

        /// <summary>
        /// Invokes DebugMessage event
        /// </summary>        
        public void InvokeDebugMessage(string message)
        {
            if (DebugMessage != null)
            {
                DebugMessage(new MessageEventArgs(message));
            }
        }

        #endregion

        #region Event DebugMessage

        /// <summary>
        /// Generates a debug message that can be added to a GUI output
        /// </summary>
        public event EventHandler TriggerViewUpdate;

        /// <summary>
        /// Invokes DebugMessage event
        /// </summary>        
        public void InvokeTriggerViewUpdate()
        {
            if (TriggerViewUpdate != null)
            {
                TriggerViewUpdate(this, null);
            }
        }

        #endregion

        

        public WindowMode CurrentMode { get; set; }

        public Person CurrentPerson { get; set; }

        public PersonalDataWindow()
        {
            InitializeComponent();
            CurrentMode = WindowMode.Create;
        }

        public PersonalDataWindow(Person p, WindowMode mode)
            : this()
        {
            UpdateControlsFromPerson(p);
            CurrentPerson = p;
            CurrentMode = mode;
        }

        /// <summary>
        /// Do nothing, close this window
        /// </summary>        
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            InvokeDebugMessage("Action canceled.");
            this.Close();
        }

        /// <summary>
        /// Button OK handler, creates a new person
        /// </summary>        
        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            // Create a new Person object            
            bool res;
            string successMsg;
            string successFailed;

            switch (CurrentMode)
            {
                case WindowMode.Create:
                    {
                        //Create a database record
                        Person person = CreatFromControls();
                        res = person.Create();
                        successMsg = "Person was added to the database.";
                        successFailed = "Failed to add a person to the database.";
                        break;
                    }
                case WindowMode.Edit:
                    {
                        Person person = CreatFromControls();
                        person.PersonId = CurrentPerson.PersonId;
                        //Create a database record
                        res = person.Update();
                        successMsg = "Person edited.";
                        successFailed = "Failed to edit a person in the database.";
                        break;
                    }
                default:
                    throw new NotImplementedException();
            }

            if (res)
            {
                InvokeDebugMessage(successMsg);
                InvokeTriggerViewUpdate();
                this.Close();
            }
            else
            {
                InvokeDebugMessage(successFailed);
            }
        }

        private Person CreatFromControls()
        {
            // Create a new Person object
            Person person = new Person()
            {
                FirstName = PeronalDataCtrl.PersonalFirstName,
                LastName = PeronalDataCtrl.PersonalLastName,
                Age = PeronalDataCtrl.PersonalAge,
                Gender = PeronalDataCtrl.PersonalGender,
                BMI = PeronalDataCtrl.PersonalBMI,
                Height = PeronalDataCtrl.PersonalHeight,
                Weight = PeronalDataCtrl.PersonalWeight,
                Desease = PeronalDataCtrl.PersonalDesease
            };

            return person;
        }

        private void UpdateControlsFromPerson(Person p)
        {
            PeronalDataCtrl.PersonalFirstName = p.FirstName;
            PeronalDataCtrl.PersonalLastName = p.LastName;
            PeronalDataCtrl.PersonalAge = p.Age;
            PeronalDataCtrl.PersonalGender = p.Gender;
            PeronalDataCtrl.PersonalBMI = p.BMI;
            PeronalDataCtrl.PersonalHeight = p.Height;
            PeronalDataCtrl.PersonalWeight = p.Weight;
            PeronalDataCtrl.PersonalDesease = p.Desease;
        }
    }
}
